package com.android.driveuploader.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by srikanthg on 9/9/14.
 */
public class ImagesDBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "wallpaper";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_GDRIVE = "gdrive";
    public static final String PARENT_ID = "folderId";
    public static final String IMAGE_URL = "imagesUrl";
    public static final String LOCATION = "location";

    public static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_GDRIVE + " (" +
                    PARENT_ID + " TEXT, " +
                    IMAGE_URL + " TEXT, " +
                    LOCATION + " TEXT)"; //TODO: should the semi-colon be deleted?
    public static final String SELECT_TABLE = "SELECT  * FROM " + TABLE_GDRIVE;

    public ImagesDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insertFile(String folderId, String imageUrl, String location) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(PARENT_ID, folderId);
        contentValues.put(IMAGE_URL, imageUrl);
        contentValues.put(LOCATION, location);
        database.insert(TABLE_GDRIVE, null, contentValues);
        database.close();
    }

    public ArrayList<HashMap<String, String>> getAllImages() {

        // ArrayList that contains every row in the database
        // and each row key / value stored in a HashMap
        ArrayList<HashMap<String, String>> contactArrayList;
        contactArrayList = new ArrayList<HashMap<String, String>>();
        String selectQuery = SELECT_TABLE;

        // Open a database for reading and writing
        SQLiteDatabase database = this.getWritableDatabase();

        // Cursor provides read and write access for the
        // data returned from a database query
        // rawQuery executes the query and returns the result as a Cursor

        Cursor cursor = database.rawQuery(selectQuery, null);

        // Move to the first row

        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> contactMap = new HashMap<String, String>();

                // Store the key / value pairs in a HashMap
                // Access the Cursor data by index that is in the same order
                // as used when creating the table

                contactMap.put(PARENT_ID, cursor.getString(0));
                contactMap.put(IMAGE_URL, cursor.getString(1));
                contactMap.put(LOCATION, cursor.getString(2));
                contactArrayList.add(contactMap);
            } while (cursor.moveToNext()); // Move Cursor to the next row
        }

        // return contact list
        return contactArrayList;
    }

}